
## V appendix table a12   N by group --------------------------
do_N_by_sample <-function(data_all = dt_all,
                        data_cnr = dt_cnr ){
N_all_by_groups <-   merge( 
    x =   as.data.frame(
      addmargins(table(died = ifelse(data_all$DMG_died_within_365d == "1","Decedent","Survivor"),
                       group = ifelse(data_all$hashed_id_num%%modulo_type == 0,"Train","Test" ),
                       age = ifelse(data_all$DMG_age<65 , "25-64","65p")) )
    ) %>%
      mutate(Freq = scales::comma(Freq)) %>%
      rename(N_All = Freq), 
    
    y = as.data.frame(
      addmargins(table(died = ifelse(data_cnr$DMG_died_within_365d == "1","Decedent","Survivor"),
                       group = ifelse(data_cnr$hashed_id_num%%modulo_type == 0,"Train","Test" ),
                       age = ifelse(data_cnr$DMG_age<65 , "25-64","65p")) )
    )  %>%
      mutate(Freq = scales::comma(Freq)) %>%
      rename(N_Cancer = Freq), 
    
    by = c("died", "group"  , "age"  )
  ) 

write.csv(N_all_by_groups, file = "N_by_grps.csv")
N_all_by_groups
} 
